package per.dj.nettysocketspring.message;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import per.dj.nettysocketspring.mapper.ClientInfoMapper;
import per.dj.nettysocketspring.model.ClientInfo;

import java.util.Date;
import java.util.UUID;

@Component
public class MessageEventHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageEventHandler.class);

    private final SocketIOServer server;

    @Autowired
    private ClientInfoMapper clientInfoMapper;

    @Autowired
    public MessageEventHandler(SocketIOServer server){
        this.server = server;
    }

    @OnConnect
    public void onConnect(SocketIOClient client)
    {
        String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
        String targetClientId = client.getHandshakeData().getSingleUrlParam("targetClientId");
        LOGGER.info("正在连接:" + clientId);
        ClientInfo clientInfo = clientInfoMapper.selectById(clientId);
        if(clientInfo != null){
            clientInfo.setConnected(1);
            clientInfo.setLastConnectedDate(new Date());
            clientInfo.setMostSignBits(client.getSessionId().getMostSignificantBits());
            clientInfo.setLeastSignBits(client.getSessionId().getLeastSignificantBits());
            clientInfoMapper.updateSelective(clientInfo);
        }else {
            ClientInfo newClientInfo = new ClientInfo();
            newClientInfo.setId(clientId);
            newClientInfo.setConnected(1);
            newClientInfo.setMostSignBits(client.getSessionId().getMostSignificantBits());
            newClientInfo.setLeastSignBits(client.getSessionId().getLeastSignificantBits());
            newClientInfo.setLastConnectedDate(new Date());
            clientInfoMapper.insert(newClientInfo);
        }
    }

    //添加@OnDisconnect事件，客户端断开连接时调用，刷新客户端信息
    @OnDisconnect
    public void onDisconnect(SocketIOClient client)
    {
        String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
        LOGGER.info("断开连接:" + clientId);
        ClientInfo clientInfo = clientInfoMapper.selectById(clientId);
        if(clientInfo != null){
            clientInfo.setConnected(0);
            clientInfo.setLastConnectedDate(new Date());
            clientInfo.setMostSignBits(null);
            clientInfo.setLeastSignBits(null);
            clientInfoMapper.updateSelective(clientInfo);
        }
    }

    @OnEvent(value = "messageevent")
    public void onEvent(SocketIOClient client, AckRequest request, MessageInfo data)
    {
        String clientId = data.getSourceClientId();
        ClientInfo clientInfo = clientInfoMapper.selectById(clientId);
        if (clientInfo != null && clientInfo.getConnected() != 0)
        {
            UUID uuid = new UUID(clientInfo.getMostSignBits(), clientInfo.getLeastSignBits());
            System.out.println(uuid.toString());
            MessageInfo sendData = new MessageInfo();
            sendData.setSourceClientId(data.getSourceClientId());
            sendData.setTargetClientId(data.getTargetClientId());
            sendData.setMsgType("chat");
            sendData.setMsgContent(data.getMsgContent());
            client.sendEvent("messageevent", sendData);
//            server.getClient(uuid).sendEvent("messageevent", sendData);
        }
    }



}
